package com.example.weather.data

import android.annotation.SuppressLint
import android.widget.Toast
import androidx.lifecycle.SavedStateHandle
import com.example.library_base.base.BaseViewModel
import com.example.library_base.toast.qualifiers.ToastShort
import com.example.weather.girl.GirlRepository
import com.example.weather.room.UserLocalRepository
import com.example.weather.util.BeanUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import java.util.*
import javax.inject.Inject

@SuppressLint("StaticFieldLeak")
@HiltViewModel
class LocalDataViewModel @Inject constructor(
    private val repository: GirlRepository,
    private val localRepository: UserLocalRepository,
    private val savedStateHandle: SavedStateHandle
): BaseViewModel<UserModel>(){

    @Inject
    @ToastShort
    lateinit var toast: Toast

    fun insertData(userModel: UserModel){
        val userDB = BeanUtils.userToUserDB(userModel)

        if (userDB.userName.isNullOrBlank()){
            toast.setText("用户姓名不能为空")
            toast.show()
            return
        }

        val now = System.currentTimeMillis()
        userDB.createTime = Date(now)
        userDB.updateTime = Date(now)

        launchUI {
            val result = localRepository.insertUserDB(userDB)
            result?.let {
                toast.setText("插入数据成功")
                toast.show()
            }?: run {
                toast.setText("插入数据失败")
                toast.show()
            }
        }
    }
}